package v4.file;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import v4.text.TextJmConfig;

public class FileJmConfig extends TextJmConfig {
	
	private Map<String, String> hardpasswordMap;
	private String hash = "SHA-1";
	
	@Override
	public void config(Properties config) {
		super.config(config);
		String hardpassword = config.getProperty("HARDPASSWORD");
		this.initHardpasswordMap(hardpassword);
		String hash = config.getProperty("HASH");
		if(hash!=null) {
			this.hash = hash;
		}
	}
	
	public void initHardpasswordMap(String hardpassword) {
		if(hardpassword!=null && hardpassword.length()>0) {
			hardpasswordMap = new HashMap<String, String>();
			int LEN = rRs.length;
			if(hardpassword.length() % LEN == 0) {
				int per = hardpassword.length() / LEN;
				for(int i=0; i<hardpassword.length(); i=i+per) {
					String a = hardpassword.substring(i, i+1);
					String aa = hardpassword.substring(i, i+per);
					hardpasswordMap.put(a, aa);
				}
			}else {
				throw new RuntimeException("HARDPASSWORD error");
			}
		}
	}
	
	public String xx(char x) {
		String old = "" + x;
		if(hardpasswordMap != null) {
			return hardpasswordMap.get(old);
		}
		return old;
	}
	
	public String toHardpassword(String str) {
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<str.length(); i++) {
			sb.append(xx(str.charAt(i)));
		}
		return sb.toString();
	}
	
	public static String createHardpassword(int len) {
		if(len>0) {
			String xx = ",";
			for(int i=0; i<rRs.length; i++) {
				xx += rRs[i];
				xx += r(len);
			}
			return xx.substring(1);
		}
		return null;
	}

	public String getHash() {
		return hash;
	}

	public void setHash(String hash) {
		this.hash = hash;
	}

}
